package com.company.nuwa.mybatis.generator.plugins;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p></p>
 *
 * @author lili
 * @version 1.0: ApiPlugin
 * @date 2023/9/16 17:09
 */
public class ApiPlugin extends PluginAdapter {

    private String sdkProjectPath;

    private String serviceProjectPath;

    /**
     * This method is called after all the setXXX methods are called, but before
     * any other method is called. This allows the plugin to determine whether
     * it can run or not. For example, if the plugin requires certain properties
     * to be set, and the properties are not set, then the plugin is invalid and
     * will not run.
     *
     * @param warnings add strings to this list to specify warnings. For example, if
     *                 the plugin is invalid, you should specify why. Warnings are
     *                 reported to users after the completion of the run.
     * @return true if the plugin is in a valid state. Invalid plugins will not
     * be called
     */
    @Override
    public boolean validate(List<String> warnings) {
        // 从上下文中读取自定义参数
        sdkProjectPath = context.getProperty("sdkProjectPath");
        serviceProjectPath = context.getProperty("serviceProjectPath");

        // 验证参数是否有效，若无配置则使用默认路径
        if (StringUtils.isEmpty(sdkProjectPath)) {
            sdkProjectPath = context.getJavaClientGeneratorConfiguration().getTargetProject();
        }
        if (StringUtils.isEmpty(serviceProjectPath)) {
            serviceProjectPath = context.getJavaModelGeneratorConfiguration().getTargetProject();
        }
        return true;
    }

    /**
     * 生成额外java文件
     */
    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<GeneratedJavaFile>(1);

        // 使用提供的路径或默认路径生成文件
        //DomainAddRequest.java
        addFile(ApiExtPlugin.addRequestFile(introspectedTable, context, sdkProjectPath), generatedJavaFiles, true);
        //DomainUpdateRequest.java
        addFile(ApiExtPlugin.updateRequestFile(introspectedTable, context, sdkProjectPath), generatedJavaFiles, true);
        //DomainSelectPageRequest
        addFile(ApiExtPlugin.selectPageFile(introspectedTable, context, sdkProjectPath), generatedJavaFiles, true);
        //DomainDTO
        addFile(ApiExtPlugin.dtoFile(introspectedTable, context, sdkProjectPath), generatedJavaFiles, true);
        //selectByIdsRequest
        addFile(ApiExtPlugin.selectByIdsFile(introspectedTable, context, sdkProjectPath), generatedJavaFiles, true);
        //facade
        addFile(FacadePlugin.facadeFile(introspectedTable, context, sdkProjectPath), generatedJavaFiles, false);
        //hystrix
        addFile(HystrixPlugin.hystrixPluginFile(introspectedTable, context, sdkProjectPath), generatedJavaFiles, false);


        // facadeImpl和service等文件使用serviceProjectPath
        //facadeImpl
        addFile(FacadeImplPlugin.facadeFile(introspectedTable, context), generatedJavaFiles, false);
        //service
        addFile(ServicePlugin.serviceFile(introspectedTable, context), generatedJavaFiles, false);
        //serviceImpl
        addFile(ServiceImplPlugin.serviceFile(introspectedTable, context), generatedJavaFiles,
            false);
        //convertFile
        addFile(ConverterPlugin.convertFile(introspectedTable, context), generatedJavaFiles, false);
        return generatedJavaFiles;
    }

    private void addFile(GeneratedJavaFile generatedJavaFile,
                         List<GeneratedJavaFile> generatedJavaFiles, boolean isOverwrite) {
        if (ApiExtPluginUtils.isExistExtFile(generatedJavaFile.getTargetProject(),
            generatedJavaFile.getTargetPackage(), generatedJavaFile.getFileName())) {
            System.out.println("文件已经存在不生成:" + generatedJavaFile.getFileName());
             return;
        }
        if (Objects.nonNull(generatedJavaFile)) {
            generatedJavaFiles.add(generatedJavaFile);
        }
    }

    @Override
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
        //重新生成代码,xml内容覆盖
        sqlMap.setMergeable(false);
        return super.sqlMapGenerated(sqlMap, introspectedTable);
    }
}
